/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import javax.ejb.Stateless;
import Entity.Address;
import Entity.SalesLead;
import Entity.SalesItem;
import Entity.SystemUser;
import Entity.SalesInquiry;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author cheeseng
 */
@Stateless
public class SalesInquirySession implements SalesInquirySessionLocal {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Long createSaleInquiry(Long leadId, String contactPerson, String comAddress1, String comAddress2, String comPostalCode, String comCountry, String phone, String salutation, String email, Integer gst, String additionalChargeDesc, Double additionalCharge, String additionalChargeType, Double transportCharge, String transportChargeType, Long userId, ArrayList<Long> saleItem) {
        
        SalesInquiry s = new SalesInquiry();
        
        SalesLead l = em.find(SalesLead.class, leadId);
        s.setSaleLead(l);
        Address companyAddress = s.getCompanyAddress();
        companyAddress.setAddress1(comAddress1);
        companyAddress.setAddress2(comAddress2);
        companyAddress.setpostalCode(comPostalCode);
        companyAddress.setCountry(comCountry);
        
        s.setContactPerson(contactPerson);
        
        s.setPhone(phone);
        s.setEmail(email);
        s.setSalutation(salutation);
        SystemUser u = em.find(SystemUser.class, userId);
        s.setCreatedBy(u);
        
        List<SalesItem> sItemSet = new ArrayList();
        for (Long o : saleItem) {
            SalesItem sItem = em.find(SalesItem.class, o);
            if (sItem != null) {
                sItemSet.add(sItem);
            }
        }

        s.setAdditionalCharge(additionalCharge);
        s.setAdditionalChargeDesc(additionalChargeDesc);
        s.setAdditionalChargeType(additionalChargeType);
        s.setGst(gst);
        s.setTransportCharge(transportCharge);
        s.setTransportChargeType(transportChargeType);

        Date currentDate = new Date();
        Query query = em.createQuery("SELECT u.inquiryNum FROM SaleInquiry u where createdDate between " + currentDate.getYear() + "-" + currentDate.getMonth() + "-1 and " + currentDate.getYear() + "-" + currentDate.getMonth() + "-31 order by inquiryId DESC");
        String inquiryNum = "";
        if (query.getResultList().isEmpty()) {
            inquiryNum = currentDate.getMonth() + "-" + currentDate.getYear() + "-1";
        } else {
            String maxNum = (String) query.getResultList().get(0);
            int nextNum = Integer.parseInt(maxNum.substring(5)) + 1;
            inquiryNum = currentDate.getMonth() + "-" + currentDate.getYear() + "-" + nextNum;
        }

        s.setInquiryNum(inquiryNum);
        em.persist(s);
        em.flush();

        return s.getInquiryId();
    }

    @Override
    public Boolean updateSaleInquiry(Long inquiryId, Long addressId, String contactPerson, String phone, String salutation, String email, Integer gst, String additionalChargeDesc, Double additionalCharge, String additionalChargeType, Double transportCharge, String transportChargeType) {
        try {
            SalesInquiry s = em.find(SalesInquiry.class, inquiryId);
            Address a = em.find(Address.class, addressId);
            s.setCompanyAddress(a);
            s.setContactPerson(contactPerson);
            s.setPhone(phone);
            s.setSalutation(salutation);
            s.setEmail(email);
            s.setGst(gst);
            s.setAdditionalCharge(additionalCharge);
            s.setAdditionalChargeDesc(additionalChargeDesc);
            s.setAdditionalChargeType(additionalChargeType);
            s.setTransportCharge(transportCharge);
            s.setTransportChargeType(transportChargeType);

            em.persist(s);
            em.flush();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean removeSaleInquiry(Long inquiryId) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
